package com.cloudmusic.api.utils;

import javazoom.jl.decoder.Bitstream;
import javazoom.jl.decoder.Header;
import org.springframework.stereotype.Service;

import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author 83979
 * @create 2020-10-15
 * @since 1.0.0
 */
@Service
public class MusicUtil {

    public int getMusicLength(String filePath) throws Exception {
        URL urlFile = new URL(filePath);
        URLConnection con = urlFile.openConnection();
        int b = con.getContentLength();// 得到音乐文件的总长度
        BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
        Bitstream bt = new Bitstream(bis);
        Header h = bt.readFrame();
        int time = (int) h.total_ms(b);
        return (time / 1000);
    }

}
